package com.matt.controller.pad;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.domain.Children;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.DateUtil;
import com.matt.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

import static com.matt.foundation.model.ErrorCode.FILE_MAST_NOT_NULL;

;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/19
 * *******************************
 */
@RestController
@RequestMapping("/pad/upload")
@Api(tags = "上传模块 - ")
public class PadUploadController {

    @Autowired
    private UploadService uploadService;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;

    @Autowired
    private MemVerificationCache cache;



    @ApiOperation(value = "上传宝宝头像")
    @RequestMapping(value = "/children_head_img" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse uploadHeadImg(String childrenId, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MAST_NOT_NULL,"文件不能为空");
        Children children = uploadService.uploadChildrenHeadImg(childrenId, file);
        return StatusResponse.success(children);
    }

    @ApiOperation(value = "上传老师头像")
    @RequestMapping(value = "/teacher_head_img" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse teacherUploadImg(@RequestHeader(RequestConstant.TICKET)String ticket, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MAST_NOT_NULL,"文件不能为空");
        String url = uploadService.uploadTeacherHeadImg(ticket, file);
        //staff有部分头像是从钉钉获取，所以这里不拼接。
        return StatusResponse.success(url);
    }

    @ApiOperation(value = "上传记录图片")
    @RequestMapping(value = "/record_img" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse recordImages(@RequestHeader(RequestConstant.TICKET)String ticket, String recordType, Integer ObjectType, String objectId, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MAST_NOT_NULL,"文件不能为空");

        if (recordType == null || recordType.length() < 3 || recordType.length() > 20){
            return StatusResponse.error(ErrorCode.PARAM_CAN_NOT_BLANK, "参数长度错误");
        }

        StaffDTO staff = cache.getStaff(ticket);

        String url = uploadService.uploadCommonFile(staff.getId(), recordType, file);

        return StatusResponse.success(url);
    }

    @ApiOperation(value = "上传图片，每天一个文件夹")
    @RequestMapping(value = "/file" ,method = RequestMethod.POST)
    @ResponseBody
    public StatusResponse uploadFile(@RequestHeader(RequestConstant.TICKET)String ticket, String bizType, MultipartFile file) {
        if (Objects.isNull(file))
            return StatusResponse.error(FILE_MAST_NOT_NULL,"文件不能为空");

        StaffDTO staff = cache.getStaff(ticket);

        String rel = DateUtil.getToday();

        if (StringUtils.isNotEmpty(bizType)){
            if (bizType.length() < 3 || bizType.length() > 20 || bizType.contains("/")) {
                return StatusResponse.error(ErrorCode.PARAM_CAN_NOT_BLANK, "参数(或长度)错误");
            }

            rel = bizType + "/" + rel;
        }

        String url = uploadService.uploadCommonFile(staff.getId(), rel, file);

//        if (!greenContentService.doImageAction(url)){
//            throw new BusinessException(ErrorCode.CHECK_STATUS_ERROR, "包含不适合发布的内容");
//        }

        return StatusResponse.success(url);
    }

}
